package com.yd.location.module.cms.service;
gitee修改
import com.yd.location.auth.domain.User;
import com.yd.location.auth.service.UserService;
import com.yd.location.common.CommonConstant;
import com.yd.location.common.cache.RedisKeyEnum;
import com.yd.location.common.cache.RedisUtil;
import com.yd.location.common.exception.BaseException;
import com.yd.location.common.spring.mpbase.IdEntity;
import com.yd.location.common.spring.mpbase.MyService;
import com.yd.location.common.web.ParamUtil;
import com.yd.location.common.web.Resp;
import com.yd.location.module.cms.domain.FriendNotice;
import com.yd.location.module.cms.mapper.FriendNoticeMapper;
import com.yd.location.module.location.domain.Relation;
import com.yd.location.module.location.service.RelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * @author Created by zk on 2020-07-14 20:55
 * @description
 */
@Slf4j
@Service
@Transactional
public class FriendNoticeService extends MyService<FriendNoticeMapper, FriendNotice> {
    @Resource
    private UserService userService;
    @Resource
    private RelationService relationService;
    @Resource
    private RedisUtil redisUtil;

    public void addApply(String id, String phone) {
        if (ParamUtil.isBlank(phone)) {
            throw new BaseException("请填写对方手机号");
        }
        User user = redisUtil.get(RedisKeyEnum.USER, id, () -> userService.getById(id));
        if (user.getUsername().equals(phone)) {
            throw new BaseException("请勿添加自己");
        }
        User one = userService.lambdaQuery()
                .select(IdEntity::getId)
                .eq(User::getUsername, phone)
                .eq(User::getType, CommonConstant.USER_ISSUER)
                .one();
        if (one == null) {
            throw new BaseException(Resp.ResStatus.NO_REGISTER, "该用户尚未注册，提醒对方注册，无需再付款");
        }
        Integer count = relationService.lambdaQuery()
                .eq(Relation::getUserId, id)
                .eq(Relation::getFriendId, one.getId())
                .count();
        if (count > 0) {
            throw new BaseException("请勿重复添加");
        }
        count = this.lambdaQuery()
                .eq(FriendNotice::getToUserId,id)
                .eq(FriendNotice::getFromUserId,one.getId())
                .eq(FriendNotice::getType,FriendNotice.FriendNoticeType.ADD.getCode())
                .eq(FriendNotice::getAcceptFlag,0)
                .count();
        if(count > 0){
            throw new BaseException("已发送申请，请等待对方回复");
        }
        //向申请者发送好友通知
        FriendNotice f1 = new FriendNotice();
        f1.setAcceptFlag(0);
        f1.setToUserId(id);
        f1.setFromUserId(one.getId());
        f1.setType(FriendNotice.FriendNoticeType.ADD.getCode());
        this.save(f1);
        //向接收方发送好友通知
        FriendNotice f2 = new FriendNotice();
        f2.setAcceptFlag(1);
        f2.setToUserId(one.getId());
        f2.setFromUserId(id);
        f2.setType(FriendNotice.FriendNoticeType.ADD.getCode());
        this.save(f2);
    }

    /**
     * 处理申请
     * result：
     * 0-等待(发起方等待状态，无任何动作)
     * 1-处理中(接收方处理状态，可选择同意拒绝)
     * 2-已接受
     * 3-已拒绝
     */
    public void dealApply(String id, String friendNoticeId, Integer result) {
        if (ParamUtil.isBlank(result) || (result != 2 && result != 3)) {
            throw new BaseException("处理状态有误");
        }
        FriendNotice friendNotice = this.getById(friendNoticeId);
        if (friendNotice == null || !friendNotice.getToUserId().equals(id)) {
            throw new BaseException("查无该申请");
        }
        if (friendNotice.getAcceptFlag() != 1) {
            throw new BaseException("该申请为非处理中状态");
        }
        FriendNotice f2 = this.lambdaQuery()
                .eq(FriendNotice::getToUserId, friendNotice.getFromUserId())
                .eq(FriendNotice::getFromUserId, friendNotice.getToUserId())
                .eq(FriendNotice::getType,FriendNotice.FriendNoticeType.ADD.getCode())
                .eq(FriendNotice::getAcceptFlag,0)
                .one();
        friendNotice.setReadFlag(0);
        f2.setReadFlag(0);
        friendNotice.setAcceptFlag(result);
        f2.setAcceptFlag(result);
        this.updateById(friendNotice);
        this.updateById(f2);
        if (result == 2) {
            Relation r1 = new Relation();
            Relation r2 = new Relation();
            User u1 = redisUtil.get(RedisKeyEnum.USER,f2.getToUserId(),() -> userService.getById(f2.getToUserId()));
            User u2 = redisUtil.get(RedisKeyEnum.USER,f2.getFromUserId(),() -> userService.getById(f2.getFromUserId()));
            r1.setUserId(f2.getToUserId());
            r1.setFriendId(f2.getFromUserId());
            r1.setName(u2.getName());
            r2.setUserId(f2.getFromUserId());
            r2.setFriendId(f2.getToUserId());
            r2.setName(u1.getName());
            relationService.save(r1);
            relationService.save(r2);
            redisUtil.delete(RedisKeyEnum.FRIEND_LIST, f2.getToUserId());
            redisUtil.delete(RedisKeyEnum.FRIEND_LIST, f2.getFromUserId());
        }
    }
}
